// pages/comment/comment.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputData: '',
    holeID: 0,
    comments: [],
    isAnon: false
  },

  // 匿名更换函数
  handleItemChange: function (e) {
    console.log(e);
    if (e.detail.value == "false") {
      this.setData({
        isAnon: true
      })
    } else {
      this.setData({
        isAnon: false
      })
    }
    console.log(this.data.isAnon)
  },
  /**提交评论函数
   * 
   */
  submit: function () {
    if (app.globalData.userInfo == null) {
      wx.showToast({
        title: '请先登录',
        icon: 'error',
        duration: 2000
      })
      return;
    }
    var submit_data = {
      holeCommentAno: this.data.isAnon,
      holeCommentContent: this.data.inputData,
      holeId: this.data.holeID
    }
    app.wxapi('POST', "/hole/comment/new", JSON.stringify(submit_data), 'application/json', res => {
      console.log(res);
      if (res.success) {
        this.setData({
          inputData: ""
        })
        this.refresh()
      } else {
        console.log("没有回应");
      }
    }, (err) => {
      //调用服务端登录接口失败
      console.log(err.errMsg)
    })
  },
  //输入框失去焦点时触发
  bindblur: function (e) {
    console.log(e)
    this.data.inputData = e.detail.value;
  },

  //刷新函数
  refresh: function () {
    var comment_data = {
      holeID: this.data.holeID
    }
    app.wxapi('GET', "/hole/comment", comment_data, 'application/x-www-form-urlencoded', res => {
      console.log(res);
      if (res.success) {
        this.setData({
          comments: res.data
        })
        console.log(this.data.comments)
      } else {
        console.log("没有回应");
      }
    }, (err) => {
      //调用服务端登录接口失败
      console.log(err.errMsg)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      holeID: options.holeID
    })
    this.refresh();
  },
  handleInput: function(e){
    var value = e.detail.value;
    this.setData({
      inputData: value
    })
  }

})